package com.gmss.crawler.module.title.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.person.service.IPersonInfoService;
import com.gmss.crawler.module.title.domain.PersonTitle;
import com.gmss.crawler.module.title.mapper.PersonTitleMapper;
import com.gmss.crawler.module.title.service.IPersonTitleService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 人员头街表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-11-05
 */
@Service
public class PersonTitleServiceImpl extends ServiceImpl<PersonTitleMapper, PersonTitle> implements IPersonTitleService {

    @Resource
    private IPersonInfoService iPersonInfoService;

    /**
     * 查询personTitle表获取头衔，并把头衔信息保存personInfo里
     * @param map
     * @param personTitleList
     * @return
     */
    @Override
    public List<PersonInfo> getPersonTitles(Map<String, Object> map, List<PersonTitle> personTitleList) {
        List<PersonInfo> personInfoList;
        List<Long> personIds = personTitleList.stream().map(personTitle -> personTitle.getPersonId()).collect(Collectors.toList());
        map.put("personIds", personIds);
        personInfoList = iPersonInfoService.queryExportExcelData(map);
        Map<Long, PersonTitle> titleMap = personTitleList.stream().collect(Collectors.toMap(PersonTitle::getPersonId, (v) -> v));
        if (CollectionUtils.isNotEmpty(personInfoList)) {
            for (PersonInfo personInfo : personInfoList) {
                personInfo.setAcademicianTitle(titleMap.get(personInfo.getId()).getAcademicianTitle());
                personInfo.setAcademicianTitleValue(titleMap.get(personInfo.getId()).getAcademicianTitleValue());
                personInfo.setElectionYear(titleMap.get(personInfo.getId()).getElectionYear());
                personInfo.setTitleYears(titleMap.get(personInfo.getId()).getTitleYears());
            }
        }
        return personInfoList;
    }
}
